package com.example.uupaymuch.controller.lxpay;

import com.example.uupaymuch.controller.notice.wudi.PayComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class LXPayOrderController {

    @Autowired
    private PayComponent payComponent;


    private final Random random = new Random();
    private final double successProbability = 0.5; // 50% 的成功概率

    @PostMapping("/pay/v1/lxPay/createBrCode")
    public ResponseEntity<Map<String, Object>> createTransaction(@RequestBody Map<String, Object> request) throws InterruptedException {
        // 生成一个随机数，并与成功概率进行比较
        boolean isSuccess = random.nextDouble() < successProbability;
        Map<String, Object> httpResponseContent = new HashMap<>();
        Map<String, Object> response = new HashMap<>();
        // 模拟入参
        String amount = request.get("amount") != null ? request.get("amount").toString() : "";
        String expiration = request.get("expiration") != null ? request.get("expiration").toString() : "";
        String externalOrderNo = request.get("externalOrderNo") != null ? request.get("externalOrderNo").toString() : "";
        String notifyUrl = request.get("notifyUrl") != null ? request.get("notifyUrl").toString() : "";

        ZonedDateTime now = ZonedDateTime.now();
        String formattedTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"));
        if (isSuccess) {
            response.put("external_id", "b1681fe6931c4ff8a57c775d6fee4387");
//            response.put("txid", txid);
//            response.put("debtor_document_number", debtorDocumentNumber);
//            response.put("debtor_name", debtorName);
            response.put("amount", amount);
            response.put("status", "PENDING");
            response.put("payer_request", null);
            response.put("additional_information", new Object[]{}); // 空数组
            response.put("qr_code", "00020101021226810014br.gov.bcb.pix2559api-h.fakebank.com.br/api/cob/b1681fe6931c4ff8a57c775d6fee43875204000053039865802BR5913SUPERPAGAMENTOS LTDA6009SAO PAULO62070503***630442F4");
            response.put("qr_code_image_url", "https://api-h.wudi-pay.codesign.ag/qr_code/WD1727231601932M1724118185RS75L");
            response.put("expires_at", "2024-09-25T02:46:09.000000Z");
            response.put("provider", "FAKE_BANK");
            response.put("provider_account", "1");
            response.put("fee", 0.01);
            response.put("account_id", 74);
            response.put("workspace_id", 34);
            response.put("updated_at", formattedTime);
            response.put("created_at", formattedTime);
            response.put("id", 130033);

            // 模拟账户信息
            Map<String, Object> account = new HashMap<>();
            account.put("id", 74);
            account.put("name", "account 1");
            account.put("status", "ACTIVE");
            account.put("customer_id", 74);
            account.put("plan_id", 68);
            account.put("created_at", "2024-05-24T12:05:50.000000Z");
            account.put("updated_at", "2024-05-24T12:05:50.000000Z");
            account.put("workspace_id", 34);
            account.put("banking_provider_id", null);
            account.put("split_rule_id", null);
            account.put("deny_legal_entities_cash_outs", true);

            response.put("account", account);
            response.put("simulate_payment_link", "https://api-h.wudi-pay.codesign.ag/api/charges/WD1727231601932M1724118185RS75L/simulate_payment");

            httpResponseContent.put("stringBody", response);
            httpResponseContent.put("byteArrayBody", null);
            httpResponseContent.put("statusCode", 200);
            httpResponseContent.put("inputStream", null);

            // 回调
//            payComponent.doNotify("brazilpay", "wudipay", txid, 1);
            return ResponseEntity.ok(httpResponseContent);
        } else {
            // 失败的返回值
            httpResponseContent.put("stringBody", null);
            httpResponseContent.put("byteArrayBody", null);
            httpResponseContent.put("statusCode", "400");
            httpResponseContent.put("inputStream", null);

            // 回调
//            payComponent.doNotify("brazilpay", "wudipay", txid, 1);
            return ResponseEntity.status(500).body(httpResponseContent);
        }
    }

}
